﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using BTM_Client_Application.SupportClasses;

namespace BTM_Client_Application
{
    public class MapMarker
    {
        private MapMarker()
        {
            _border = new Border();
            _border.CornerRadius = new CornerRadius(4);
            _border.Background = new SolidColorBrush(Colors.GreenYellow){Opacity = 0.7};
            _border.HorizontalAlignment = HorizontalAlignment.Center;
            _border.MouseEnter += (sender, args) =>
                {
                    if(!_titleBorder.IsVisible)
                        Res.MakeOpacityAnimation(true, _titleBorder, new TimeSpan(0, 0, 0, 0, 200));
                    //_titleBorder.Visibility = Visibility.Visible;
                    Panel.SetZIndex(_titleBorder, Int32.MaxValue);
                };
            _border.MouseLeave += (sender, args) =>
                {
                    if (_titleBorder.IsVisible)
                    //_titleBorder.Visibility = Visibility.Hidden;
                        Res.HideWithAnimation(_titleBorder, new TimeSpan(0, 0, 0, 0, 200));
                };
            _panel = new StackPanel();
            _panel.Children.Add(_border);

            //_title = new Label
            //    {
            //        Foreground = new SolidColorBrush(Colors.White),
            //        HorizontalAlignment = HorizontalAlignment.Center,
            //        VerticalAlignment = VerticalAlignment.Center
            //    };
            _title = new TextBlock
                {
                    Margin = new Thickness(3, 1, 3, 1),
                    Foreground = new SolidColorBrush(Colors.White),
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            _titleBorder = new Border
                {
                    Margin = new Thickness(0, 4, 0, 0),
                    Visibility = Visibility.Hidden,
                    CornerRadius = new CornerRadius(2),
                    Background = new SolidColorBrush(Colors.Black) {Opacity = 0.7},
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    Child = _title,
                };
            _panel.Children.Add(_titleBorder);

        }

        private readonly Border _border;
        private readonly StackPanel _panel;
        private readonly Border _titleBorder;
        private readonly TextBlock _title;

        public MapMarker(Image im, string title = "") : this()
        {
            //_border.Tag = this;
            _panel.Tag = this;
            _border.Child = im;
            Title = title;
            _border.MouseEnter += (sender, args) =>
                {
                    Mouse.OverrideCursor = Cursors.Hand;
                };
            _border.MouseLeave += (sender, args) =>
                {
                    Mouse.OverrideCursor = null;
                };
            //_panel.Children.Insert(0, im);
        }

        public static UpdateMapIconDelegate UpdateMapIcon { get; set; }

        public UIElement GetMarker()
        {
            return _panel;
        }

        public void SetImage(ImageSource im)
        {
            (_border.Child as Image).Source = im;
        }

        public string Title
        {
            get { return _title.Text; }
            set { _title.Text = value; }
        }

        public Brush Background { get { return _border.Background; } set
        {
            UpdateMapIcon(() => { _border.Background = value; });
        } }
    }
}
